$(document).ready(function()
{
    SnapshotIndexView.init();
});

SnapshotIndexView = new Object();

SnapshotIndexView.props = new Object();
SnapshotIndexView.props.queue = null;

SnapshotIndexView.init = function()
{
    // init widgets
    PreferenceWidget.init(SnapshotIndexView.savePreferenceCallback);
    SnapshotProfileWidget.init();
    SnapshotNextWidget.init();

    // bind button
    $('#widget_snapshot_profile_A_83').click({isLike: 1}, SnapshotIndexView.playSnapshot);
    $('#widget_snapshot_profile_A_86').click({isLike: 0}, SnapshotIndexView.playSnapshot);

    // init snapshot
    SnapshotIndexView.initSnapshot(PHP_PARAMS.snapshot_index_view.targets);
}

SnapshotIndexView.savePreferenceCallback = function(data)
{
    SnapshotIndexView.initSnapshot(data['targets']);
}

SnapshotIndexView.initSnapshot = function(targets)
{
    // init queue
    SnapshotIndexView.props.queue = new Queue();
    for(index in targets)
    {
        var item = new SnapshotQueueItem();
        item.setData(targets[index]);
        SnapshotIndexView.props.queue.enqueue(item);
    }

    // show top
    SnapshotIndexView.showTop();
}

SnapshotIndexView.playSnapshot = function(event)
{
    // remove top item
    SnapshotIndexView.props.queue.dequeue();

    // show top
    SnapshotIndexView.showTop();

    // inc num like
    var isLike = event.data.isLike;
    if(isLike)
    {
        CounterWidget.incNumLike();
    }

    // send request
    $.ajaxQueue(
    {
        url: CONST.BASE_URL + '/ajax/playSnapshot',
        data: {isLike: isLike},
        type: 'POST',
        success: function(json)
        {
            var data = JSON.parse(json);

            // check end snapshot
            if(!data.nextTarget)
            {
                return;
            }

            // add item to queue
            var item = new SnapshotQueueItem();
            item.setData(data.nextTarget);
            SnapshotIndexView.props.queue.enqueue(item);

            // re-show buttons if they're hiding
            if($('#widget_snapshot_profile_DIV_79').not(':visible'))
            {
                $('#widget_snapshot_profile_DIV_79').show();
            }
        }
    });
}

SnapshotIndexView.showTop = function()
{
    // check empty queue
    if(SnapshotIndexView.props.queue.isEmpty())
    {
        $('#widget_snapshot_profile_DIV_79').hide();
        return;
    }

    // show snapshot profile
    var item = SnapshotIndexView.props.queue.peek();
    SnapshotProfileWidget.showTop(item);

    // show snapshot next
    var items = SnapshotIndexView.props.queue.getItems(1,3);
    SnapshotNextWidget.showTop(items);

    // re-init pretty photo
    MainLayout.initPrettyPhoto();
}

SnapshotQueueItem = function()
{
    // props
    this.props = new Object();
    this.props.id = null;
    this.props.name = null;
    this.props.birthday = null;
    this.props.profile_url = null;
    this.props.gender = {id: null, name: null};
    this.props.location = {id:null, name: null};
    this.props.hometown = {id:null, name: null};
    this.props.works = []; // {id: null, name: null}
    this.props.educations = []; // {id: null, name: null}
    this.props.avatar = {id: null, filename: null};
    this.props.photos = []; // {id: null, name: null}
}

SnapshotQueueItem.prototype.setData = function(data)
{
    this.props = data;
}
